// JavaScript Document
$(document).ready(function(){ 
	$(function() {
		//***********MODULES****************
		//send draggable data to db
		$("#sortable_content ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			var order = $(this).sortable("serialize") + '&action=updateRecordsListings&table=modules';
			$.post("modulesDB.php", order);
		}
		});
		//send checkbox "enabled" data to db
		$('input[name=enabledmodules]').change(function() {
			if ($(this).attr('value') == 1) {
					var enabled = 0; 
				}else{
					var enabled = 1;	
					}
			var module_id = $(this).parents('li').attr('id').substr(13,6);
			$(this).parents('li').fadeOut('slow', function() {	
				$.ajax({
				type: "POST",
				url: "modulesDB.php",
				data: {action:'updateEnabled', enabledVar:enabled, module_idVar:module_id,table:'modules'},
				success: function(){
   					//need to fix this, full page refresh not the best	
					location.reload();
 				 	}
				});	
			});
		});
		//*************USERS******************
		//send draggable data to db
		$("#sortable_user_content ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			var order = $(this).sortable("serialize") + '&action=updateRecordsListings&table=users';
			$.post("usersDB.php", order);
		}
		});
		//*************LINKS*******************
		//send draggable data to db
		$("#sortable_content_links ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			var order = $(this).sortable("serialize") + '&action=updateRecordsListings&table=links';
			$.post("modulesDB.php", order);
		}
		});
		//send checkbox "enabled" data to db
		$('input[name=enabledlinks]').change(function() {
			if ($(this).attr('value') == 1) {
					var enabled = 0; 
				}else{
					var enabled = 1;	
					}
			var module_id = $(this).parents('li').attr('id').substr(13,6);
			$(this).parents('li').fadeOut('slow', function() {	
				$.ajax({
				type: "POST",
				url: "modulesDB.php",
				table: "links",
				data: {action:'updateEnabled', enabledVar:enabled, module_idVar:module_id,table:'links'},
				success: function(){
   					//need to fix this, page refresh not the best for ajax but had trouble with CSS and more without reloading page	
					location.reload();
 				 	}
				});	
			});
		});
		//*************Meeting Resources*******************
		//send draggable data to db
		$("#sortable_content_meeting_resources ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			var order = $(this).sortable("serialize") + '&action=updateRecordsListings&table=meeting_resources';
			$.post("modulesDB.php", order);
		}
		});
		//send checkbox "enabled" data to db
		$('input[name=enabledmeeting_resources]').change(function() {
			if ($(this).attr('value') == 1) {
					var enabled = 0; 
				}else{
					var enabled = 1;	
					}
			var module_id = $(this).parents('li').attr('id').substr(13,6);
			$(this).parents('li').fadeOut('slow', function() {	
				$.ajax({
				type: "POST",
				url: "modulesDB.php",
				table: "meeting_resources",
				data: {action:'updateEnabled', enabledVar:enabled, module_idVar:module_id,table:'meeting_resources'},
				success: function(){
   					//need to fix this, page refresh not the best for ajax but had trouble with CSS and more without reloading page	
					location.reload();
 				 	}
				});	
			});
		});
		$('.edit').editable('inlineEdit.php', {
			 indicator : 'Saving...',
			 tooltip   : 'Click to edit...',
			 style     : 'display: inline',
			 submitdata : {table: table},
			 submit    : 'OK'
     	}); 
		$(".ajaxupload").editable("processFile.php", {
			indicator : "<img src='../images/loading.gif'>",
			type : 'ajaxupload',
			style     : 'display: inline',
			submit : 'Upload',
			cancel : 'Cancel',
			tooltip : "Click to replace...",
			submitdata : {table: 'meeting_resources'}
		});
	});
});